package process_demo.review.wait_notify;

import java.util.concurrent.TimeUnit;

/**
 * @author haomin
 * @date 2022/09/12 12:00
 **/
public class Demo3 {
    static Object o = new Object();
    static class MyThread extends Thread{
        private Object o;
        public MyThread (Object o){
            this.o = o;
        }

        @Override
        public void run() {
            synchronized (o){
                try {
                    o.wait();
                    System.out.println(getName());
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 10; i++) {
            MyThread t = new MyThread(o);
            t.start();
        }
        TimeUnit.SECONDS.sleep(5);
        synchronized (o) {
            //o.notify(); 随机唤醒一个
            o.notifyAll(); // 全部唤醒 顺序随机
        }
    }
}